# -*- coding: utf-8 -*-
"""
CSV导出模块
用于将爬取数据导出为CSV格式
"""

import csv
import os
import logging
from datetime import datetime
from typing import List, Dict, Any, Optional
import pandas as pd


class CSVExporter:
    """CSV导出器"""
    
    def __init__(self, output_dir: str = "output"):
        self.logger = logging.getLogger(__name__)
        self.output_dir = output_dir
        
        # 确保输出目录存在
        os.makedirs(output_dir, exist_ok=True)
        
        self.logger.info(f"CSV导出器初始化，输出目录: {output_dir}")
    
    def generate_filename(self, city: str, business_type: str, timestamp: str = None) -> str:
        """生成CSV文件名"""
        if timestamp is None:
            timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
        
        filename = f"{city}_{business_type}_{timestamp}.csv"
        return os.path.join(self.output_dir, filename)
    
    def export_to_csv(self, data: List[Dict[str, Any]], city: str, business_type: str, 
                     append_mode: bool = True) -> str:
        """
        导出数据到CSV文件
        
        Args:
            data: 要导出的数据列表
            city: 城市名称
            business_type: 业态名称
            append_mode: 是否使用追加模式
            
        Returns:
            str: 导出的文件路径
        """
        if not data:
            self.logger.warning("没有数据可导出")
            return ""
        
        try:
            # 生成文件名
            timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
            filepath = self.generate_filename(city, business_type, timestamp)
            
            # 准备数据
            df = pd.DataFrame(data)
            
            # 重新排列和重命名列
            column_mapping = {
                'title': '标题',
                'contact_person': '联系人',
                'contact_info': '联系方式',
                'acceptable_rent': '承受租金',
                'building_area': '建筑面积',
                'publish_date': '发布时间',
                'city': '城市',
                'business_type': '业态',
                'detail_url': '详情页URL',
                'crawl_time': '抓取时间'
            }
            
            # 选择和重命名列
            available_columns = [col for col in column_mapping.keys() if col in df.columns]
            df_export = df[available_columns].copy()
            df_export.rename(columns=column_mapping, inplace=True)
            
            # 导出到CSV
            mode = 'a' if append_mode and os.path.exists(filepath) else 'w'
            header = not (append_mode and os.path.exists(filepath))
            
            df_export.to_csv(
                filepath, 
                mode=mode, 
                header=header, 
                index=False, 
                encoding='utf-8-sig'  # 支持中文
            )
            
            self.logger.info(f"数据已导出到CSV: {filepath}")
            self.logger.info(f"导出记录数: {len(df_export)}")
            
            return filepath
            
        except Exception as e:
            self.logger.error(f"CSV导出失败: {e}")
            return ""
    
    def print_data_sample(self, data: List[Dict[str, Any]], sample_size: int = 10):
        """打印数据样本到控制台"""
        if not data:
            print("❌ 没有数据可显示")
            return
        
        print(f"\n📋 数据样本 (前{min(sample_size, len(data))}条):")
        print("=" * 80)
        
        for i, item in enumerate(data[:sample_size]):
            print(f"\n{i+1}. 标题: {item.get('title', '未知')}")
            print(f"   联系人: {item.get('contact_person', '未知')}")
            print(f"   联系方式: {item.get('contact_info', '未知')}")
            print(f"   租金: {item.get('acceptable_rent', '未知')}")
            print(f"   发布时间: {item.get('publish_date', '未知')}")
            print(f"   城市: {item.get('city', '未知')}")
            print(f"   业态: {item.get('business_type', '未知')}")
    
    def get_export_stats(self, data: List[Dict[str, Any]], filepath: str) -> Dict[str, Any]:
        """获取导出统计信息"""
        file_size = 0
        if os.path.exists(filepath):
            file_size = os.path.getsize(filepath)
        
        return {
            'record_count': len(data),
            'file_path': filepath,
            'file_size_kb': round(file_size / 1024, 2),
            'export_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        }
    
    def export_summary_report(self, city_business_results: Dict[str, Dict[str, Any]]) -> str:
        """导出汇总报告"""
        try:
            timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
            summary_file = os.path.join(self.output_dir, f"爬取汇总报告_{timestamp}.csv")
            
            summary_data = []
            
            for city, business_results in city_business_results.items():
                for business_type, result in business_results.items():
                    summary_data.append({
                        '城市': city,
                        '业态': business_type,
                        '数据量': result.get('data_count', 0),
                        '成功状态': '成功' if result.get('success', False) else '失败',
                        '文件路径': result.get('csv_file', ''),
                        '导出时间': result.get('export_time', '')
                    })
            
            if summary_data:
                df_summary = pd.DataFrame(summary_data)
                df_summary.to_csv(summary_file, index=False, encoding='utf-8-sig')
                
                self.logger.info(f"汇总报告已导出: {summary_file}")
                return summary_file
            
        except Exception as e:
            self.logger.error(f"导出汇总报告失败: {e}")
        
        return ""


class DataDisplayManager:
    """数据显示管理器"""
    
    def __init__(self):
        self.logger = logging.getLogger(__name__)
    
    def print_crawl_progress(self, city: str, business_type: str, 
                           current_data_count: int, total_expected: int = None):
        """打印爬取进度"""
        progress_info = f"🔄 正在爬取: {city} - {business_type}"
        if total_expected:
            progress_info += f" ({current_data_count}/{total_expected})"
        else:
            progress_info += f" (已获取 {current_data_count} 条)"
        
        print(progress_info)
    
    def print_completion_summary(self, city: str, business_type: str, 
                               final_count: int, csv_file: str, 
                               filter_stats: Dict[str, Any] = None):
        """打印完成摘要"""
        print(f"\n✅ {city} - {business_type} 爬取完成!")
        print(f"   📊 最终数据量: {final_count} 条")
        print(f"   📁 CSV文件: {csv_file}")
        
        if filter_stats:
            print(f"   🕒 时间过滤: 截止 {filter_stats.get('cutoff_date', '未知')} ({filter_stats.get('days_limit', 30)}天内)")
            if filter_stats.get('excluded_count', 0) > 0:
                print(f"   🗑️ 过滤掉: {filter_stats['excluded_count']} 条过期数据")
        
        file_size = 0
        if os.path.exists(csv_file):
            file_size = os.path.getsize(csv_file) / 1024
        print(f"   💾 文件大小: {file_size:.1f} KB")
    
    def print_multi_city_summary(self, results: Dict[str, Any]):
        """打印多城市爬取汇总"""
        print(f"\n🎉 多城市爬取完成!")
        print("=" * 60)
        
        total_data = 0
        successful_combinations = 0
        total_combinations = 0
        
        for city, city_result in results.get('results', {}).items():
            print(f"\n🏙️ {city}:")
            city_total = city_result.get('total_data_count', 0)
            city_success_rate = city_result.get('success_rate', 0)
            
            print(f"   总数据量: {city_total} 条")
            print(f"   成功率: {city_success_rate:.1f}%")
            
            for business_type, business_result in city_result.get('business_results', {}).items():
                total_combinations += 1
                if business_result.get('success', False):
                    successful_combinations += 1
                    data_count = business_result.get('data_count', 0)
                    total_data += data_count
                    print(f"   ✅ {business_type}: {data_count} 条")
                    
                    # 显示CSV文件信息
                    csv_file = business_result.get('csv_file', '')
                    if csv_file:
                        print(f"      📁 {os.path.basename(csv_file)}")
                else:
                    print(f"   ❌ {business_type}: 失败")
        
        print(f"\n📈 总体统计:")
        print(f"   总数据量: {total_data} 条")
        print(f"   成功组合: {successful_combinations}/{total_combinations}")
        overall_success_rate = (successful_combinations / total_combinations * 100) if total_combinations > 0 else 0
        print(f"   总体成功率: {overall_success_rate:.1f}%")
